// 高阶函数实现模板模式

const Beverage = (param) => {
    const boilWater = () => console.log('把水煮沸');

    const brew = param.brew || function() {
        throw new Error('必须传递 brew 方法')
    };
    const pourInCup = param.pourInCup || function() {
        throw new Error('必须传递 pourInCup 方法')
    };
    const addCondiments = param.addCondiments || function() {
        throw new Error('必须传递 addCondiments 方法')
    };

    let F = function() {};
    F.prototype.init = () => {
        boilWater();
        brew();
        pourInCup();
        addCondiments();
    }
    return F;
}

// test
const Coffee = Beverage({
    brew() {
        console.log('用沸水冲泡咖啡');
    },
    pourInCup() {
        console.log('把咖啡倒进杯子');
    },
    addCondiments() {
        console.log('加糖和牛奶');
    }
});
let coffee = new Coffee();
coffee.init();
